﻿@{
    ViewBag.Title = "选择标段";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}

<!--标段是属于项目的，此处是否应该先选择项目，再选择标段，且支持选择多标段-->
<script type="text/javascript">
    $(function () {
        //当页面首次刷新的时候执行的事件
        initProjectTable();  //项目列表
        initBidTable();  //招标列表
        initSectionTable();  //标段列表
    });
    //实现对DataGird控件的绑定操作
    //初始化项目列表
    function initProjectTable(queryData) {
        $('#projectDataGrid').datagrid({
            url: '/ProjectInfo/ProjectInfoList',
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: true,
            queryParams: queryData,
            toolbar: '#projectToolBar',
            columns: [[
                { title: '项目名称', field: 'ProjectName', width: 180, sortable: true },
            ]],
            onClickRow: function (index, row) {
                var selectedRow = $("#projectDataGrid").datagrid("getSelected");
                if (selectedRow) {
                    $('#bidDataGrid').datagrid('load', { projectID: selectedRow.ID });
                }
            }
        });
    }

    //初始化招标列表
    function initBidTable(queryData) {
        $('#bidDataGrid').datagrid({
            url: '/BidInfo/GetBidsByProjectID',
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            queryParams: queryData,
            toolbar: '#bidToolBar',
            columns: [[
                //{ field: 'ck', checkbox: true },   //选择
                { title: '招标标题', field: 'BidTitle', width: 180, sortable: true },
            ]],
            onClickRow: function (index, row) {
                var selectedRow = $("#bidDataGrid").datagrid("getSelected");
                if (selectedRow) {
                    $('#sectionDataGrid').datagrid('load', { bidID: selectedRow.ID, correctiveSectionIDs: $("#tempCorrectiveSectionIDs").val() });
                }
            }
        });
    }

    //初始化标段列表
    function initSectionTable(queryData) {
        $('#sectionDataGrid').datagrid({
            url: '/BidInfo/GetSectionList',
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: true,
            queryParams: queryData,  //异步查询的参数
            toolbar: '#sectionToolBar',
            columns: [[
                {
                    field: 'IsChecked',
                    formatter: function (value, row, index) {
                        if (value == true) {
                            return '<input type="checkbox" checked="true" onclick="ChooseBid.ChangeChecked(' + index + ');" />';
                        }
                        else {
                            return '<input type="checkbox" onclick="ChooseBid.ChangeChecked(' + index + ');" />';
                        }
                    }
                },
                { title: '标段名称', field: 'SectionName', width: 200, sortable: true },
            ]]
        });
    }

    var ChooseBid = {
        //刷新项目
        ReloadProject: function () {
            $("#projectDataGrid").datagrid("reload");
        },

        //刷新招标
        ReloadBid: function () {
            $("#bidDataGrid").datagrid("reload");
        },

        //刷新标段
        ReloadSection: function () {
            $("#sectionDataGrid").datagrid("reload");
        },

        //全选
        ChooseAll: function () {
            //var selectedRow = $("#projectDataGrid").datagrid("getSelected");  //获取选中行记录
            //if (selectedRow) {
            //    $.post("/Purview/ChooseAll?menuID=" + selectedRow.ID + "&isChecked=true");
            //    ChooseBid.ReloadBid();
            //    ChooseBid.ReloadSection();
            //}

            //var dataRows = $("#projectDataGrid").datagrid("getRows");
            //if (dataRows != null && dataRows.length > 0) {
            //    for
            //}
            //var dataRows = $("#projectDataGrid").datagrid("getRows");
            //var i=0;
            //for(i=0;i<dataRows.length,i++){
            //    ChangeChecked(i);
            //}
            var dataRows = $("#sectionDataGrid").datagrid("getRows");
            var i = 0;
            for (i = 0; i <= dataRows.length; i++) {
                //dataRows[i].IsChecked = true;
                ChooseBid.ChangeChecked(i);
            }
        },

        //全不选
        UnChooseAll: function () {
            var selectedRow = $("#projectDataGrid").datagrid("getSelected");  //获取选中行记录
            if (selectedRow) {
                $.post("/Purview/ChooseAll?menuID=" + selectedRow.ID + "&isChecked=false");
                ChooseBid.ReloadBid();
                ChooseBid.ReloadSection();
            }
        },

        //反选
        OppositeChoose: function () {
            var selectedRow = $("#projectDataGrid").datagrid("getSelected");  //获取选中行记录
            if (selectedRow) {
                $.post("/Purview/OppositeChoose?menuID=" + selectedRow.ID);
                ChooseBid.ReloadBid();
                ChooseBid.ReloadSection();
            }
        },

        //勾选或者取消勾选
        ChangeChecked: function (index) {
            //debugger;
            $("#sectionDataGrid").datagrid("selectRow", index);  //选中指定行
            var selectedRow = $("#sectionDataGrid").datagrid("getSelected");  //获取选中行记录
            if (selectedRow) {
                //var correctiveSectionIDs = $("#correctiveSectionIDs").val();  //取得当前的标段列表ID集合（格式：ID1,ID2,ID3）
                var tempCorrectiveSectionIDs = $("#tempCorrectiveSectionIDs").val();
                //$("#tempCorrectiveSectionIDs").val(correctiveSectionIDs);
                var correctiveSectionIDSplist = [];
                if (tempCorrectiveSectionIDs.length > 0) {
                    correctiveSectionIDSplist = tempCorrectiveSectionIDs.split(",");  //转为数组
                }

                //此处是为了取出IsChecked的值
                if (selectedRow.IsChecked == false) {
                    selectedRow.IsChecked = true;
                    //$.EasyuiSlide("不勾选");

                    //如果元素不存在，则添加到数组中
                    if (tempCorrectiveSectionIDs.indexOf(selectedRow.ID) < 0) {
                        correctiveSectionIDSplist.push(selectedRow.ID);
                    }
                }
                else {
                    selectedRow.IsChecked = false;
                    //$.EasyuiSlide("勾选");

                    //如果元素存在，则从数组中移除
                    if (tempCorrectiveSectionIDs.indexOf(selectedRow.ID) >= 0) {
                        correctiveSectionIDSplist = $.RemoveElement(correctiveSectionIDSplist, selectedRow.ID);
                    }
                }
                //$.post("/Purview/ChangeChecked?menuActionID=" + selectedRow.ID + "&isChecked=" + selectedRow.IsChecked);  //xxxx由上面的方法获取
                $("#tempCorrectiveSectionIDs").val(correctiveSectionIDSplist.join(","));
            }
            //$.EasyuiSlide($("#tempCorrectiveSectionIDs").val());  //测试用
            ////ChooseBid.ReloadSection();
        },
    }

</script>

<table>
    <tr>
        <!--左边为项目列表-->
        <td>
            <div region="west" data-options="border:false" style="width: 212px; height: 400px;">
                <div class="easyui-panel" title="项目列表">
                    <table id="projectDataGrid" style="height: 372px;"></table>
                    <div id="projectToolBar">
                        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='ChooseBid.ReloadProject()' data-options='plain:true,iconCls:"icon-application_lightning"'>刷新</a>
                    </div>
                </div>
            </div>
        </td>

        <!--中间为招标列表-->
        <td>
            <div region="center" style="border-left: 0px; width: 212px; height: 400px;">
                <div class="easyui-panel" title="招标列表">
                    <table id="bidDataGrid" style="height: 372px; "></table>
                    <div id="bidToolBar">
                        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='ChooseBid.ReloadBid()' data-options='plain:true,iconCls:"icon-application_lightning"'>刷新</a>
                    </div>
                </div>
            </div>
        </td>

        <!--右边为权限路径预览-->
        <td>
            <div region="center" style="border-left: 0px; width: 265px; height: 400px;">
                <div class="easyui-panel" title="标段列表">
                    <table id="sectionDataGrid" style="height: 372px; "></table>
                    <div id="sectionToolBar">
                        @*<a href='javascript:void(0)' class='easyui-linkbutton' onclick='ChooseBid.ChooseAll()' data-options='plain:true,iconCls:"icon-bestseller"'>全选</a>
                        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='ChooseBid.UnChooseAll()' data-options='plain:true,iconCls:"icon-blue_lego"'>全不选</a>
                        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='ChooseBid.OppositeChoose()' data-options='plain:true,iconCls:"icon-caution"'>反选</a>*@
                        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='ChooseBid.ReloadSection()' data-options='plain:true,iconCls:"icon-application_lightning"'>刷新</a>
                    </div>
                </div>
            </div>
        </td>
    </tr>
</table>
